package com.aspose.cells;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class AutoFilter {
   private Worksheet f;
   Object a;
   DataSorter b;
   CellArea c;
   private String g;
   boolean d = false;
   FilterColumnCollection e;

   Worksheet a() {
      return this.f;
   }

   AutoFilter(Worksheet sheet, Object parent) {
      this.f = sheet;
      this.c = new CellArea();
      this.a = parent;
      this.e = new FilterColumnCollection(this);
   }

   CellArea b() {
      CellArea var1 = new CellArea();
      var1.StartRow = this.c.StartRow + 1;
      var1.EndRow = this.f.getCells().getMaxRow();
      var1.StartColumn = this.c.StartColumn;
      var1.EndColumn = this.c.EndColumn;
      if (this.a != null) {
         if (this.a instanceof ListObject) {
            ListObject var2 = (ListObject)this.a;
            var1.StartRow = var2.getStartRow() + 1;
            var1.EndRow = var2.getEndRow();
            if (var2.j()) {
               --var1.EndRow;
            }
         } else if (this.a instanceof PivotTable) {
            PivotTable var3 = (PivotTable)this.a;
            var1.StartRow = var3.n;
            var1.EndRow = var3.j;
         }
      }

      return var1;
   }

   public DataSorter getSorter() {
      if (this.b == null) {
         this.b = new DataSorter(this);
      }

      return this.b;
   }

   void a(AutoFilter var1) {
      this.g = var1.g;
      if (var1.g != null) {
         this.c = CellArea.c(var1.c);
         this.e.a(var1.e);
      }

   }

   public void setRange(int row, int startColumn, int endColumn) {
      this.g = CellsHelper.cellIndexToName(row, startColumn) + ":" + CellsHelper.cellIndexToName(row, endColumn);
      this.c.StartRow = row;
      this.c.EndRow = row;
      this.c.StartColumn = startColumn;
      this.c.EndColumn = endColumn;
   }

   public String getRange() {
      return this.g;
   }

   public void setRange(String value) {
      this.g = value;
      if (value != null && !"".equals(value)) {
         this.i();
      } else {
         this.e.clear();
      }

   }

   void c() {
      if (this.g != null && !"".equals(this.g)) {
         int var1 = this.c.StartRow;
         zani var2 = this.f.getCells().q();

         for(int var3 = this.c.StartColumn; var3 <= this.c.EndColumn; ++var3) {
            for(int var4 = 0; var4 < var2.getCount(); ++var4) {
               CellArea var5 = var2.a(var4);
               if (var1 >= var5.StartRow && var1 <= var5.EndRow && var3 >= var5.StartColumn && var3 <= var5.EndColumn) {
                  int var6 = var5.EndColumn;
                  if (var5.EndColumn > this.c.EndColumn) {
                     var6 = this.c.EndColumn;
                  }

                  int var7 = var5.StartColumn;
                  if (var5.StartColumn < this.c.StartColumn) {
                     var7 = this.c.StartColumn;
                  }

                  for(int var8 = var7; var8 < var6; ++var8) {
                     FilterColumn var9 = this.getFilterColumns().get(var8 - this.c.StartColumn);
                     var9.b(false);
                  }

                  var3 = var5.EndColumn;
                  break;
               }
            }
         }

      }
   }

   void a(Name var1) {
      Range var2 = var1.getRange();
      if (var2 != null) {
         this.c.d(var2.a());
         this.g = CellsHelper.cellIndexToName(this.c.StartRow, this.c.StartColumn) + ":" + CellsHelper.cellIndexToName(this.c.EndRow, this.c.EndColumn);
      }
   }

   public void addFilter(int fieldIndex, String criteria) {
      if (fieldIndex >= 0 && fieldIndex <= this.c.EndColumn - this.c.StartColumn) {
         FilterColumn var3 = this.e.get(fieldIndex);
         var3.b(criteria);
         this.d = true;
      } else {
         throw new CellsException(6, "Field index is out of range.");
      }
   }

   public void addDateFilter(int fieldIndex, int dateTimeGroupingType, int year, int month, int day, int hour, int minute, int second) {
      FilterColumn var9 = this.e.get(fieldIndex);
      var9.b(fieldIndex, dateTimeGroupingType, year, month, day, hour, minute, second);
      this.d = true;
   }

   public void removeDateFilter(int fieldIndex, int dateTimeGroupingType, int year, int month, int day, int hour, int minute, int second) {
      FilterColumn var9 = this.e.get(fieldIndex);
      var9.a(fieldIndex, dateTimeGroupingType, year, month, day, hour, minute, second);
      this.d = true;
   }

   public void removeFilter(int fieldIndex, String criteria) {
      FilterColumn var3 = this.e.get(fieldIndex);
      var3.a(criteria);
      this.d = true;
   }

   public void filter(int fieldIndex, String criteria) {
      if (fieldIndex >= 0 && fieldIndex <= this.c.EndColumn - this.c.StartColumn) {
         FilterColumn var3 = this.e.get(fieldIndex);
         var3.c(criteria);
         this.d = true;
      } else {
         throw new CellsException(6, "Field index is out of range.");
      }
   }

   public void filterTop10(int fieldIndex, boolean isTop, boolean isPercent, int itemCount) {
      if (fieldIndex >= 0 && fieldIndex <= this.c.EndColumn - this.c.StartColumn) {
         FilterColumn var5 = this.e.get(fieldIndex);
         var5.a(isTop, isPercent, itemCount);
         this.d = true;
      } else {
         throw new CellsException(6, "Field index is out of range.");
      }
   }

   public void dynamicFilter(int fieldIndex, int dynamicFilterType) {
      FilterColumn var3 = this.e.get(fieldIndex);
      var3.a(dynamicFilterType);
      this.d = true;
   }

   public void addFontColorFilter(int fieldIndex, CellsColor color) {
      FilterColumn var3 = this.e.get(fieldIndex);
      var3.a(color);
      this.d = true;
   }

   public void addFillColorFilter(int fieldIndex, int pattern, CellsColor foregroundColor, CellsColor backgroundColor) {
      FilterColumn var5 = this.e.get(fieldIndex);
      var5.a(pattern, foregroundColor, backgroundColor);
      this.d = true;
   }

   public void addIconFilter(int fieldIndex, int iconSetType, int iconId) {
      FilterColumn var4 = this.e.get(fieldIndex);
      var4.a(iconSetType, iconId);
      this.d = true;
   }

   public void matchBlanks(int fieldIndex) {
      if (fieldIndex >= 0 && fieldIndex <= this.c.EndColumn - this.c.StartColumn) {
         FilterColumn var2 = this.e.get(fieldIndex);
         var2.d();
         this.d = true;
      } else {
         throw new CellsException(6, "Field index is out of range.");
      }
   }

   public void matchNonBlanks(int fieldIndex) {
      if (fieldIndex >= 0 && fieldIndex <= this.c.EndColumn - this.c.StartColumn) {
         FilterColumn var2 = this.e.get(fieldIndex);
         var2.e();
         this.d = true;
      } else {
         throw new CellsException(6, "Field index is out of range.");
      }
   }

   public void custom(int fieldIndex, int operatorType1, Object criteria1) {
      if (criteria1 == null) {
         throw new CellsException(6, "The custom criteria of auto filter could not be null");
      } else {
         this.custom(fieldIndex, operatorType1, criteria1, false, 6, (Object)null);
         this.d = true;
      }
   }

   public void custom(int fieldIndex, int operatorType1, Object criteria1, boolean isAnd, int operatorType2, Object criteria2) {
      if (fieldIndex >= 0 && fieldIndex <= this.c.EndColumn - this.c.StartColumn) {
         if (operatorType2 != 6 && criteria2 == null) {
            throw new CellsException(6, "The custom criteria of auto filter could not be null");
         } else {
            FilterColumn var7 = this.e.get(fieldIndex);
            var7.a(operatorType1, criteria1, isAnd, operatorType2, criteria2);
            this.d = true;
         }
      } else {
         throw new CellsException(6, "Field index is out of range.");
      }
   }

   public void showAll() {
      this.e.clear();
      Cells var1 = this.f.getCells();

      for(int var2 = 0; var2 < var1.getRows().getCount(); ++var2) {
         Row var3 = var1.getRows().getRowByIndex(var2);
         if (var3.isHidden()) {
            var3.setHidden(false);
         }
      }

   }

   public void removeFilter(int fieldIndex) {
      for(int var2 = 0; var2 < this.e.getCount(); ++var2) {
         FilterColumn var3 = this.e.getByIndex(var2);
         if (var3.getFieldIndex() == fieldIndex) {
            this.e.removeAt(var2);
            return;
         }
      }

   }

   boolean a(Object var1) {
      for(int var2 = 0; var2 < this.e.getCount(); ++var2) {
         FilterColumn var3 = this.e.getByIndex(var2);
         if (!var3.a(var1)) {
            return true;
         }
      }

      return false;
   }

   public void refresh() {
      if (this.e.getCount() != 0) {
         Cells var1 = this.f.getCells();
         int var2 = this.c.StartRow + 1;
         boolean var3 = false;
         int var4 = this.c.StartColumn;
         int var5 = var1.getMaxDataRow();
         if (var5 == -1) {
            var5 = 0;
         }

         int var6 = this.c.EndRow;
         int var7 = this.c.EndRow;

         int var8;
         Row var9;
         for(var8 = 0; var8 < var1.getRows().getCount(); ++var8) {
            var9 = var1.getRows().getRowByIndex(var8);
            if (var9.getIndex() >= var2) {
               if (var9.getIndex() > var5) {
                  break;
               }

               if (var9.f() == 0.0D || var9.isHidden()) {
                  var1.unhideRow(var9.getIndex(), var1.getStandardHeight());
               }

               if (var9.getIndex() > var6 && !var9.isBlank()) {
                  if (var6 + 1 == var9.getIndex()) {
                     var7 = var9.getIndex();
                  }

                  var6 = var9.getIndex();
               }
            }
         }

         int var22;
         for(var8 = 0; var8 < this.e.getCount(); ++var8) {
            FilterColumn var23 = this.e.getByIndex(var8);
            switch(var23.getFilterType()) {
            case 2:
               ((DynamicFilter)var23.getFilter()).a(var1, var4 + var23.getFieldIndex(), var2, var7);
            }

            if (var23.getFilterType() == 5) {
               var22 = var4 + var23.getFieldIndex();
               com.aspose.cells.b.a.a.zd var10 = new com.aspose.cells.b.a.a.zd();
               int var11 = 0;

               for(var2 = this.c.StartRow + 1; var2 <= var7; ++var2) {
                  Cell var12 = var1.checkCell(var2, var22);
                  if (var12 == null) {
                     var1.hideRow(var2);
                  } else if (var12.getType() == 4 || var12.getType() == 1) {
                     ++var11;
                     double var13 = var12.getDoubleValue();
                     ArrayList var15 = (ArrayList)var10.d((Object)var13);
                     if (var15 == null) {
                        var15 = new ArrayList();
                        var10.b(var13, var15);
                     }

                     com.aspose.cells.b.a.a.ze.a(var15, (Object)var2);
                  }
               }

               Top10Filter var27 = (Top10Filter)var23.getFilter();
               boolean var28 = var27.isTop();
               boolean var14 = var27.isPercent();
               int var29 = var27.getItems();
               if (var14) {
                  var29 = var11 * var29 / 100;
               }

               List var16;
               int var17;
               boolean var18;
               ArrayList var19;
               Iterator var20;
               int var21;
               int var30;
               if (!var28) {
                  var16 = var10.e();
                  var17 = 0;
                  var18 = false;

                  for(var30 = 0; var30 < var10.b(); ++var30) {
                     var19 = (ArrayList)var16.get(var30);
                     var17 += var19.size();
                     if (var17 >= var29) {
                        ++var30;
                        var27.setCriteria(var1.a((Integer)var19.get(0), var22, false).getDoubleValue());
                        break;
                     }
                  }

                  while(var30 < var16.size()) {
                     var19 = (ArrayList)var16.get(var30);
                     var20 = var19.iterator();

                     while(var20.hasNext()) {
                        var21 = (Integer)var20.next();
                        var1.hideRow(var21);
                     }

                     ++var30;
                  }
               } else {
                  var16 = var10.e();
                  var17 = 0;
                  var18 = false;

                  for(var30 = var10.b() - 1; var30 >= 0; --var30) {
                     var19 = (ArrayList)var16.get(var30);
                     var17 += var19.size();
                     if (var17 > var29) {
                        --var30;
                        var27.setCriteria(var1.a((Integer)var19.get(0), var22, false).getDoubleValue());
                        break;
                     }
                  }

                  while(var30 >= 0) {
                     var19 = (ArrayList)var16.get(var30);
                     var20 = var19.iterator();

                     while(var20.hasNext()) {
                        var21 = (Integer)var20.next();
                        var1.hideRow(var21);
                     }

                     --var30;
                  }
               }
            }
         }

         for(var2 = this.c.StartRow + 1; var2 <= var7; ++var2) {
            var8 = var1.getRows().d(var2);
            if (var8 != -1) {
               var9 = var1.getRows().getRowByIndex(var8);
               if (var9.isHidden() || var9.f() == 0.0D) {
                  continue;
               }
            }

            for(int var24 = 0; var24 < this.e.getCount(); ++var24) {
               FilterColumn var25 = this.e.getByIndex(var24);
               if (var25.getFilterType() != 5) {
                  var22 = var4 + var25.getFieldIndex();
                  Cell var26 = var1.checkCell(var2, var22);
                  if (!var25.a(var26, var2, var22)) {
                     var1.hideRow(var2);
                     break;
                  }
               }
            }
         }

         this.d = false;
      }
   }

   public FilterColumnCollection getFilterColumns() {
      if (this.e == null) {
         this.e = new FilterColumnCollection(this);
      }

      return this.e;
   }

   /** @deprecated */
   public FilterColumnCollection getFilterColumnCollection() {
      if (this.e == null) {
         this.e = new FilterColumnCollection(this);
      }

      return this.e;
   }

   boolean d() {
      return this.e != null && this.e.getCount() > 0;
   }

   int e() {
      if (this.g != null && !"".equals(this.g)) {
         int var1 = this.c.EndColumn - this.c.StartColumn + 1;
         int var2 = this.c.StartRow;
         zani var3 = this.f.getCells().q();

         for(int var4 = this.c.StartColumn; var4 <= this.c.EndColumn; ++var4) {
            for(int var5 = 0; var5 < var3.getCount(); ++var5) {
               CellArea var6 = var3.a(var5);
               if (var2 >= var6.StartRow && var2 <= var6.EndRow && var4 >= var6.StartColumn && var4 <= var6.EndColumn) {
                  int var7 = var6.EndColumn;
                  if (var6.EndColumn > this.c.EndColumn) {
                     var7 = this.c.EndColumn;
                  }

                  int var8 = var6.StartColumn;
                  if (var6.StartColumn < this.c.StartColumn) {
                     var8 = this.c.StartColumn;
                  }

                  var1 -= var7 - var8;
                  var4 = var6.EndColumn;
                  break;
               }
            }
         }

         return var1;
      } else {
         return 0;
      }
   }

   int f() {
      return this.g != null && !"".equals(this.g) ? this.c.EndColumn - this.c.StartColumn + 1 : 0;
   }

   int g() {
      return this.c.StartRow;
   }

   CellArea h() {
      return this.c;
   }

   void a(int var1, int var2) {
      CellArea var3 = this.c;
      if (var2 < 0 && var3.StartRow >= var1 && var3.StartRow <= var1 - var2 - 1) {
         this.g = null;
         this.showAll();
      } else {
         boolean var4 = false;
         boolean[] var5 = new boolean[]{var4};
         var3 = zayd.a(var3, var1, var2, var5);
         var4 = var5[0];
         if (var4) {
            this.g = null;
            this.showAll();
         } else {
            this.c = var3;
            this.g = CellsHelper.cellIndexToName(var3.StartRow, var3.StartColumn) + ":" + CellsHelper.cellIndexToName(var3.EndRow, var3.EndColumn);
         }
      }
   }

   void b(int var1, int var2) {
      CellArea var3 = this.c;
      int var5;
      if (var2 > 0) {
         if (var1 <= this.c.StartColumn) {
            var3.StartColumn += var2;
            var3.EndColumn += var2;
            this.c = var3;
            this.g = CellsHelper.cellIndexToName(var3.StartRow, var3.StartColumn) + ":" + CellsHelper.cellIndexToName(var3.EndRow, var3.EndColumn);
         } else if (var1 <= this.c.EndColumn) {
            var3.EndColumn += var2;
            this.c = var3;
            this.g = CellsHelper.cellIndexToName(var3.StartRow, var3.StartColumn) + ":" + CellsHelper.cellIndexToName(var3.EndRow, var3.EndColumn);
            int var4 = var1 - var3.StartColumn;

            for(var5 = 0; var5 < this.e.getCount(); ++var5) {
               FilterColumn var6 = this.e.getByIndex(var5);
               if (var6.getFieldIndex() >= var4) {
                  var6.setFieldIndex(var6.getFieldIndex() + var2);
               }
            }
         }
      } else {
         boolean var10 = false;
         var5 = var1 - var2 - 1;
         int var7;
         FilterColumn var8;
         int var11;
         if (var1 <= this.c.StartColumn) {
            if (var5 < this.c.StartColumn) {
               var3.StartColumn += var2;
               var3.EndColumn += var2;
               this.c = var3;
               this.g = CellsHelper.cellIndexToName(var3.StartRow, var3.StartColumn) + ":" + CellsHelper.cellIndexToName(var3.EndRow, var3.EndColumn);
            } else if (var5 <= this.c.EndColumn) {
               var11 = var5 - var3.StartColumn;
               var3.StartColumn = var1;
               var3.EndColumn += var2;
               this.c = var3;
               this.g = CellsHelper.cellIndexToName(var3.StartRow, var3.StartColumn) + ":" + CellsHelper.cellIndexToName(var3.EndRow, var3.EndColumn);
               this.d = true;

               for(var7 = 0; var7 < this.e.getCount(); ++var7) {
                  var8 = this.e.getByIndex(var7);
                  if (var8.getFieldIndex() <= var11) {
                     this.e.removeAt(var7--);
                     var10 = true;
                  } else {
                     var8.setFieldIndex(var8.getFieldIndex() + var2);
                  }
               }
            } else {
               this.g = null;
            }
         } else if (var1 < this.c.EndColumn) {
            if (var5 < this.c.EndColumn) {
               var11 = var1 - var3.StartColumn;
               var7 = var11 - var2 - 1;
               var3.EndColumn += var2;
               this.c = var3;
               this.g = CellsHelper.cellIndexToName(var3.StartRow, var3.StartColumn) + ":" + CellsHelper.cellIndexToName(var3.EndRow, var3.EndColumn);

               for(int var12 = 0; var12 < this.e.getCount(); ++var12) {
                  FilterColumn var9 = this.e.getByIndex(var12);
                  if (var9.getFieldIndex() >= var11) {
                     if (var9.getFieldIndex() <= var7) {
                        this.e.removeAt(var12--);
                     } else {
                        var9.setFieldIndex(var9.getFieldIndex() + var2);
                     }
                  }
               }
            } else {
               var11 = var1 - var3.StartColumn;
               var3.EndColumn += var5 - 1;
               this.c = var3;
               this.g = CellsHelper.cellIndexToName(var3.StartRow, var3.StartColumn) + ":" + CellsHelper.cellIndexToName(var3.EndRow, var3.EndColumn);

               for(var7 = 0; var7 < this.e.getCount(); ++var7) {
                  var8 = this.e.getByIndex(var7);
                  if (var8.getFieldIndex() >= var11) {
                     this.e.removeAt(var7--);
                  }
               }
            }
         }

         if (var10 && this.e.getCount() == 0) {
            this.showAll();
         }
      }

   }

   private void i() {
      if (this.g != null && !"".equals(this.g)) {
         byte var1 = 0;
         byte var2 = 0;
         int[] var4;
         int var8;
         int var9;
         if (this.g.indexOf(58) == -1) {
            try {
               int[] var3 = new int[]{var1};
               var4 = new int[]{var2};
               CellsHelper.a(com.aspose.cells.b.a.zv.a(this.g, "$", ""), var3, var4);
               var8 = var3[0];
               var9 = var4[0];
               this.c.StartColumn = var9;
               this.c.EndColumn = var9;
               this.c.StartRow = var8;
               this.c.EndRow = var8;
            } catch (Exception var7) {
               throw new CellsException(6, "Range of AutoFilter is not valid.");
            }
         } else {
            String[] var10 = com.aspose.cells.b.a.zv.d(this.g, ':');
            if (var10.length != 2) {
               throw new CellsException(6, "Range of AutoFilter is not valid.");
            }

            try {
               var4 = new int[]{var1};
               int[] var5 = new int[]{var2};
               CellsHelper.a(com.aspose.cells.b.a.zv.a(var10[0], "$", ""), var4, var5);
               var8 = var4[0];
               var9 = var5[0];
               this.c.StartColumn = var9;
               this.c.StartRow = var8;
               var4[0] = var8;
               var5[0] = var9;
               CellsHelper.a(com.aspose.cells.b.a.zv.a(var10[1], "$", ""), var4, var5);
               var8 = var4[0];
               var9 = var5[0];
               this.c.EndColumn = var9;
               this.c.EndRow = var8;
            } catch (Exception var6) {
               throw new CellsException(6, "Range of AutoFilter is not valid.");
            }

            if (this.c.StartRow > this.c.EndRow) {
               if (this.c.StartColumn >= this.c.EndColumn) {
                  var8 = this.c.StartRow;
                  this.c.StartRow = this.c.EndRow;
                  this.c.EndRow = var8;
                  var9 = this.c.StartColumn;
                  this.c.StartColumn = this.c.EndColumn;
                  this.c.EndColumn = var9;
               } else {
                  var8 = this.c.StartRow;
                  this.c.StartRow = this.c.EndRow;
                  this.c.EndRow = var8;
               }
            } else if (this.c.StartRow == this.c.EndRow) {
               if (this.c.StartColumn > this.c.EndColumn) {
                  var9 = this.c.StartColumn;
                  this.c.StartColumn = this.c.EndColumn;
                  this.c.EndColumn = var9;
               }
            } else if (this.c.StartColumn > this.c.EndColumn) {
               var9 = this.c.StartColumn;
               this.c.StartColumn = this.c.EndColumn;
               this.c.EndColumn = var9;
            }
         }

      } else {
         throw new CellsException(6, "Range of AutoFilter is not set.");
      }
   }

   static String b(Object var0) {
      switch(zamo.a(var0.getClass())) {
      case 3:
         if ((Boolean)var0) {
            return "True";
         }

         return "False";
      case 9:
         return zase.y((Integer)var0);
      case 14:
         return zase.b((Double)var0);
      case 18:
         return com.aspose.cells.b.a.zr.a(var0);
      default:
         return com.aspose.cells.b.a.zr.a(var0);
      }
   }
}
